<template>
  <div class="">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1 form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="brName">
          <a-input v-model:value="formState.brName" class="w-64" placeholder="客户姓名/助记码/手机号" />
        </a-form-item>

        <a-form-item name="dateRange" label="登记日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>

        <a-form-item name="empName" label="">
          <a-input v-model:value="formState.empName" class="w-64 ml-2" placeholder="医师" />
        </a-form-item>

        <a-form-item name="itemName" label="理疗项目">
          <a-input v-model:value="formState.itemName" class="w-64" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onAdd"> 登记 </a-button>
        <BaseExport
          type="理疗"
          name="理疗登记"
          code="00001137"
          :search-params="{ startTime: formState.dateRange?.[0], endTime: formState.dateRange?.[1], ...formState }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :data-source="tableData" fill :loading="loading" column-code="00001137" :scroll="{ x: 2000 }" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'lsh'">
            <PopoverTable :config="detailConfig" :width="600" :params="{ lsh: record.lsh }">{{ record.lsh }}</PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-tag :color="statusEnum[record.status].color">{{ statusEnum[record.status].text }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'djrq'"> {{ record.yyrq + record.yysj }} </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton confirm-text="确认结束？" type="link" size="small" :disabled="record.status !== 0" @click="onChangeEnd(record, 1)"> 结束 </BaseButton>
              <BaseButton confirm-text="确认撤销？" type="link" danger size="small" :disabled="record.status !== 0" @click="onCLickOver(record.lsh, 2)">
                撤销
              </BaseButton>
              <!-- <a-button type="link" size="small" :disabled="record.status !== 0" @click="onAdd(record)">修改</a-button> -->
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import useStore from '../store'
import Edit from './Edit'

const store = useStore()
const editOpen = ref(false)
const rowData = ref({})
const statusEnum = [
  { text: '等候中', color: 'green' },
  { text: '已结束', color: 'blue' },
  { text: '撤销', color: 'red' },
  { text: '服务中', color: 'blue' }
]
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const onAdd = (row = {}) => {
  editOpen.value = true
  rowData.value = { ...row }
}
const onSuccess = () => {
  getData()
}
const onLoadEdit = (record) => {
  rowData.value = record
  editOpen.value = true
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/course/courseRegister/getCourseRegiserList',
  immediate: true,
  formatParams: () => {
    return {
      startTime: formState.value.dateRange?.[0],
      endTime: formState.value.dateRange?.[1],
      ...formState.value
    }
  }
})
const detailConfig = ref({
  url: '/course/courseRegister/getCourseRegiser',
  columnCode: '00001138',
  rowKey: 'itemCode',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const onChangeEnd = (item) => {
  if (item.status === 3) {
    onCLickOver(item.lsh, 1)
  } else {
    onCLickOver(item.lsh, 3)
    onCLickOver(item.lsh, 1)
  }
}

const onCLickOver = async (lsh, status) => {
  const res = await post('course/courseRegister/doCancelCourseRegister', { lsh, status })
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  if (status === 1) {
    message.success(res.msg || '操作成功')
  }
  getData()
}

onMounted(() => {
  getData()
  if (store.currentRegistrants?.brId) {
    onLoadEdit(store.currentRegistrants)
  }
})
</script>
